package com.hb.gp.impl;

import java.util.Map;
import java.util.Random;

import com.hb.gp.GPInsCommand;
import com.hb.gp.GPType;
import com.hb.gp.IGPCommandMessageService;
import com.hb.gp.util.tools.GP_Tools;

/**
 * 
 * @author shangzk
 *
 */
@GPType(INS = GPInsCommand.INS_50)
public class GPInitializeUpdate implements IGPCommandMessageService {
	private static final String hexString = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F";
	private static final String[] hexs = hexString.split(",");
	private static final int hexLength = 16;

	@Override
	public String makeApduMap(String apdu, Map<String, Object> map) {
		// TODO Auto-generated method stub
		// 圈存的操作
		if (apdu.subSequence(6, 8).equals("02")) {
			return ePurse(map);
		} else {
			return makeApduMap(apdu, null, map);
		}
	}

	private String ePurse(Map<String, Object> map) {
		int blance = Integer.parseInt(String.valueOf(map.get(VARIBLE_BLANCE)));
		String apdu = "805000020b01" + String.format("%08x", blance)
				+ "999999999999";
		return apdu;
	}

	@Override
	public String makeApduMap(String apdu, Map<String, Object> varibleMap,
			Map<String, Object> storeMap) {
		// TODO Auto-generated method stub
		String data = "";
		for (int i = 0; i < hexLength; i++) {
			data += hexs[new Random().nextInt(hexLength)];
		}
		storeMap.put(VARIBLE_RANDOM, data);
		String request = "8050" + apdu.substring(4, 8)
				+ GP_Tools.getHexLength(data) + data;
		return request;
	}

}
